﻿namespace FFF.Test.Application.AppServices.PublicService.Sessions
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    using Abp.Application.Services;
    using Abp.Auditing;
    using Abp.Dependency;
    using Abp.Json;
    using Abp.Runtime.Caching;
    using Abp.UI;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    using FFF.Test.Entity;
    using FFF.Test.Entity.FW.View;
    using FFF.Test.Util.Consts;
    using Ydm.Extensions;

    /// <summary>
    /// session的管理对象
    /// </summary>
    public class SessionAppService : YdmAbpAppServiceBase, ISessionAppService
    {
        /// <summary>
        /// 依赖注入容器
        /// </summary>
        public IIocResolver IocResolver { get; set; }

        /// <summary>
        /// 依赖注入容器
        /// </summary>
        public ICacheManager CacheManager { get; set; }

        /// <summary>
        /// http上下文
        /// </summary>
        public IHttpContextAccessor HttpContext { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="SessionAppService"/> class.
        /// session构造方法
        /// </summary>
        /// <param name="fWUserRepository">用户仓储</param>
        public SessionAppService()
        {
        }

        /// <summary>
        /// 获取当前登录的接口的第三方的信息
        /// </summary>
        /// <returns>当前登录人的信息</returns>
        public async Task<object> GetInterfaceUserAsync()
        {
            if (base.AbpSession.UserId != null)
            {
                long userLoginId = base.AbpSession.UserId.Value;

                // 插入缓存
                string userDto = await this.CacheManager
                     .GetCache(CacheAppConsts.UserStoreCache)
                     .GetAsync(userLoginId.ToString(), () => Task.FromResult("123")
                     );
                return userDto;
            }
            else
            {
                throw new UserFriendlyException("未找到接口调用方信息");
            }
        }

        /// <summary>
        /// 获取当前验证码
        /// </summary>
        /// <returns>验证码</returns>
        public async Task<string> GetValidateCode()
        {
            var validateCode = this.HttpContext.HttpContext.Session.GetString(AppConfigConsts.ValidateCodeStr).ToLower();
            return await Task.FromResult(validateCode);
        }

        /// <summary>
        /// 获取当前登陆人
        /// </summary>
        /// <returns>验证码</returns>
        public async Task<ViewFWUser> GetCurrentUserInfo()
        {
            var userinfo = this.HttpContext.HttpContext.Session.GetUserInfo();
            return await Task.FromResult(userinfo);
        }

        /// <summary>
        /// 获取短信验证码
        /// </summary>
        /// <returns>验证码</returns>
        public async Task<string> GetSmmsCode()
        {
            var ssmCode = this.HttpContext.HttpContext.Session.GetString(AppConfigConsts.SsmsCodeStr);
            if (ssmCode.IsNullOrEmpty())
            {
                throw new UserFriendlyException("请先输入验证码");
            }

            ssmCode = ssmCode.ToLower();

            return await Task.FromResult(ssmCode);
        }
    }
}
